//
//  FLPrivacyAlertView.m
//  Cat
//
//  Created by lushitong on 2018/12/23.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "FLPrivacyAlertView.h"
#import "HttpWebManager.h"

@interface FLPrivacyAlertView()
@property (nonatomic, copy) NSString *addressURL;
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, copy) void(^clickAgreeButtonAction)(FLPrivacyAlertView *view);
@property (nonatomic, copy) void(^clickDisAgressAction)(void);

@property (nonatomic, strong) UIView *whiteCenterView;
@property (nonatomic, strong) UILabel *privacyLabel;
@property (nonatomic, strong) UIWebView *privacyView;
@property (nonatomic, strong) UIView *bottomVerticalLineView;
@property (nonatomic, strong) UIView *bottomLineView;
@property (nonatomic, strong) UIButton *agreeButton;
@property (nonatomic, strong) UIButton *disAgreeButton;


@end
@implementation FLPrivacyAlertView

+ (instancetype)showInView:(UIView *)superView
	clickAgreeButtonAction:(void (^)(FLPrivacyAlertView *view))clickAgreeButtonAction
	   clickDisAgreeAction:(void (^)(void))clickDisAgressAction {
	return [self showInView:superView loadHtmlAddress:@"" clickAgreeButtonAction:clickAgreeButtonAction clickDisAgreeAction:clickDisAgressAction];
}

+ (instancetype)showInView:(UIView *)superView
		   loadHtmlAddress:(NSString *)addressURL
	clickAgreeButtonAction:(void (^)(FLPrivacyAlertView *))clickAgreeButtonAction
	   clickDisAgreeAction:(void (^)(void))clickDisAgressAction {
	
	FLPrivacyAlertView *aletView = [[FLPrivacyAlertView alloc] initWithAddressURL:addressURL];
	aletView.clickAgreeButtonAction = clickAgreeButtonAction;
	aletView.clickDisAgressAction = clickDisAgressAction;
	aletView.addressURL = [addressURL length] > 0 ? addressURL : [HttpWebManager getYinSi];
	[aletView showInView:superView];
	return aletView;
}

- (void)showInView:(UIView *)view {
	[view addSubview:self];
	[view bringSubviewToFront:self];
}

- (instancetype)initWithAddressURL:(NSString *)addressURL {
	self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
	if (self) {
		self.addressURL = [addressURL length] > 0 ? addressURL :[HttpWebManager getYinSi];
		[self loadSubView];
	}
	return self;
}

- (void)loadSubView {
	[self addSubview:self.bgView];
	[self addSubviews:@[self.whiteCenterView,
						self.privacyLabel,
						self.privacyView,
						self.bottomLineView,
						self.bottomVerticalLineView,
						self.agreeButton,
						self.disAgreeButton]];
	
	[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.edges.equalTo(self);
	}];
	
	[self.whiteCenterView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.mas_left).offset(kQKFitModuleDecimal(40));
		make.right.equalTo(self.mas_right).offset(-kQKFitModuleDecimal(40));
		make.centerX.equalTo(self.bgView.mas_centerX);
		make.centerY.equalTo(self.bgView.mas_centerY);
		make.size.mas_equalTo(CGSizeMake(ScreenWidth - kQKFitModuleDecimal(80), kQKFitModuleDecimal(430)));
	}];
	
	[self.privacyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.whiteCenterView.mas_top).offset(kQKFitModuleDecimal(15));
		make.centerX.equalTo(self.whiteCenterView.mas_centerX);
	}];
	
	[self.privacyView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.whiteCenterView.mas_left).offset(kQKFitModuleDecimal(14));
		make.right.equalTo(self.whiteCenterView.mas_right).offset(kQKFitModuleDecimal(-14));
		make.top.equalTo(self.whiteCenterView.mas_top).offset(kQKFitModuleDecimal(50));
		make.bottom.equalTo(self.whiteCenterView.mas_bottom).offset(-kQKFitModuleDecimal(45.5));
	}];
	
	[self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.whiteCenterView.mas_left);
		make.right.equalTo(self.whiteCenterView.mas_right);
		make.height.equalTo(@(kQKFitModuleDecimal(1)));
		make.top.equalTo(self.whiteCenterView.mas_bottom).offset(-kQKFitModuleDecimal(44));
	}];
	
	[self.bottomVerticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.mas_equalTo(CGSizeMake(kQKFitModuleDecimal(1), kQKFitModuleDecimal(30))).priorityHigh();
		make.top.equalTo(self.bottomLineView.mas_bottom).offset(kQKFitModuleDecimal(7));
		make.centerX.equalTo(self.whiteCenterView.mas_centerX);
	}];
	
	[self.disAgreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.whiteCenterView.mas_left);
		make.right.equalTo(self.bottomVerticalLineView.mas_left);
		make.top.equalTo(self.bottomLineView.mas_top);
		make.bottom.equalTo(self.whiteCenterView.mas_bottom);
	}];
	
	[self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.bottomLineView.mas_top);
		make.left.equalTo(self.disAgreeButton.mas_right);
		make.right.equalTo(self.whiteCenterView.mas_right);
		make.bottom.equalTo(self.whiteCenterView.mas_bottom);
	}];
	[self dealWebView];
	[self loadRequest];
	
}

- (void)dealWebView {
	//取消右侧，下侧滚动条，去处上下滚动边界的黑色背景
	for (UIView *_aView in [self.privacyView subviews]) {
		if ([_aView isKindOfClass:[UIScrollView class]]) {
			[(UIScrollView *)_aView setShowsVerticalScrollIndicator:NO];
			//右侧的滚动条
			[(UIScrollView *)_aView setShowsHorizontalScrollIndicator:NO];
			//下侧的滚动条
			for (UIView *_inScrollview in _aView.subviews) {
				if ([_inScrollview isKindOfClass:[UIImageView class]]) {
					_inScrollview.hidden = YES;  //上下滚动出边界时的黑色的图片
				}
			}
		}
	}
}

- (void)loadRequest {
	NSURLQueryItem *sidQueryItem = [NSURLQueryItem queryItemWithName:@"appname" value:[[NSString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]] base64Encode]];
	
	NSURLComponents *components = [[NSURLComponents alloc] initWithString:self.addressURL];
	NSMutableArray *queryItems = [NSMutableArray arrayWithArray:components.queryItems];
	[queryItems addObject:sidQueryItem];
	components.queryItems = queryItems;
	
	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:components.URL];
	[request setHTTPShouldHandleCookies:YES];
	[self.privacyView loadRequest:request];
}

#pragma mark----- action 的方法 -----

- (void)disAgreeButtonTiAction:(UIButton *)button {
	//TODO: 不同意应该怎么处理？
	if (self.clickDisAgressAction) {
		self.clickDisAgressAction();
	}
	[self dismiss];
}

- (void)agressButtonTiAction:(UIButton *)button {
	if (self.clickAgreeButtonAction) {
		self.clickAgreeButtonAction(self);
	}
	[self dismiss];
}

- (void)dismiss {
	[self removeFromSuperview];
}

#pragma mark----- lazy init -----

- (UIView *)bgView {
	if (!_bgView) {
		_bgView = [[UIView alloc] init];
		_bgView.backgroundColor = COLOR_WITH_HEX_A(0x000000, 0.33);
	}
	return _bgView;
}

- (UILabel *)privacyLabel {
	if (!_privacyLabel) {
		_privacyLabel = [UILabel labelWithText:@"隐私政策" font:[UIFont systemFontOfSize:kQKFitFont(18)] textColor:COLOR_WITH_HEX(0x000000)];
	}
	return _privacyLabel;
}

- (UIWebView *)privacyView {
	if (!_privacyView) {
		_privacyView = [[UIWebView alloc] init];
		_privacyView.backgroundColor = [UIColor clearColor];
		_privacyView.opaque = NO;
	}
	return _privacyView;
}

- (UIView *)bottomLineView {
	if (!_bottomLineView) {
		_bottomLineView = [[UIView alloc] init];
		_bottomLineView.backgroundColor = COLOR_WITH_HEX(0xD9D9D9);
	}
	return _bottomLineView;
}

- (UIView *)bottomVerticalLineView {
	if (!_bottomVerticalLineView) {
		_bottomVerticalLineView = [[UIView alloc] init];
		_bottomVerticalLineView.backgroundColor = COLOR_WITH_HEX(0xD9D9D9);
	}
	return _bottomVerticalLineView;
}

- (UIView *)whiteCenterView {
	if (!_whiteCenterView) {
		_whiteCenterView = [[UIView alloc] init];
		_whiteCenterView.backgroundColor = [UIColor whiteColor];
		_whiteCenterView.layer.cornerRadius = kQKFitModuleDecimal(15);
	}
	return _whiteCenterView;
}

- (UIButton *)disAgreeButton {
	if (!_disAgreeButton) {
		_disAgreeButton = [UIButton buttonWithTitle:@"不同意" color:COLOR_WITH_HEX(0x666666) font:[UIFont systemFontOfSize:kQKFitFont(16)]];
		[_disAgreeButton addTarget:self action:@selector(disAgreeButtonTiAction:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _disAgreeButton;
}

- (UIButton *)agreeButton {
	if (!_agreeButton) {
		_agreeButton = [UIButton buttonWithTitle:@"同意" color:COLOR_WITH_HEX(0x4A90E2) font:[UIFont boldSystemFontOfSize:kQKFitFont(16)]];
		[_agreeButton addTarget:self action:@selector(agressButtonTiAction:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _agreeButton;
}

@end
